from django.db.models import Q
from django.shortcuts import render
from rest_framework.pagination import PageNumberPagination
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet
from paths.sers import *


# Create your views here.
class PathsPagination(PageNumberPagination):
    page_size_query_param = 'page_size'
    page_size = None


class PathsModelViewSet(ModelViewSet):
    queryset = Paths.objects.all()
    serializer_class = PathsModelSerializer

    ordering_fields = ['follow']  # 可以按照follow排序了, ordering=follow   -follow

    pagination_class = PathsPagination


class StageModelViewSet(ModelViewSet):
    queryset = Stage.objects.all()
    serializer_class = StageModelSerializer


class JoinPathAPIView(APIView):
    permission_classes = (IsAuthenticated,)  # 认证用户才能访问

    def get(self, request):
        # 获取用户是否关注此路径
        # 1. 获取user, pid
        user = request.user
        pid = request.GET.get('pid')
        # 2. 校验参数
        if pid:
            pass
        else:
            return Response({'code': '999', 'msg': '缺少路径id'})

        # 3. 查找
        count = UserPath.objects.filter(user=user, path_id=pid).count()

        # 4. 返回
        return Response({'code': '0', 'msg': '查询成功', 'count': count})

    def post(self, request):
        # 1. 获取参数, user, pid
        user = request.user
        pid = request.data.get('pid')

        # 2. 校验参数
        if pid:
            pass
        else:
            return Response({'code': '999', 'msg': '缺少路径id'})

        # 3. 增加关系
        try:
            up = UserPath.objects.get(user=user, path_id=pid)
        except Exception as e:
            up = None

        if up:
            up.path.follow -= 1
            up.path.save()
            up.delete()
            return Response({'code': '0', 'msg': '取消加入'})
        else:
            up = UserPath.objects.create(user=user, path_id=pid)
            up.path.follow += 1
            up.path.save()
            # 4. 返回结果
            return Response({'code': '0', 'msg': '加入成功'})


class PathCommentModelViewSet(ModelViewSet):
    queryset = PathComment.objects.all()
    serializer_class = PathCommentModelSerializer

    filter_fields = ('path',)


class PathCommentAPIView(APIView):
    permission_classes = (IsAuthenticated,)

    def post(self, request):
        # 1. 获取数据,  user, pid 路径id, content 评论内容, to:给那个评论回复, 评论id
        user = request.user
        pid = request.data.get('pid')
        content = request.data.get('content')
        to = request.data.get('to')

        # 2. 校验数据
        if all([pid, content]):
            pass
        else:
            return Response({'code': '999', 'msg': '缺少参数'})

        try:
            path = Paths.objects.get(id=pid)
        except Exception as e:
            return Response({'code': '999', 'msg': '路径不存在'})

        # 3. 保存数据
        PathComment.objects.create(
            user=user,
            path=path,
            content=content,
            to_id=to if to else None)
        # 4. 返回结果
        return Response({'code': '0', 'msg': '评论成功'})


